﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using WindowsClient.Classes;
using System.Net;
using System.IO;
using System.Xml;
using System.Web;

namespace WindowsClient
{
    public partial class Home : Form
    {
        String[] QueryTypes = { "GET", "POST" };

        public Home()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Result r;
            String response = null;
            ASCIIEncoding encoding = new ASCIIEncoding();
            Uri inputURI;

            // Reset the output
            textBox2.Text = "Waiting for response ...";

            // Check for url
            if(!Uri.TryCreate(textBox1.Text,UriKind.Absolute,out inputURI))
            {
                textBox2.Text = "Invalid URL";
                return;
            }

            // Retrieve response
            if (comboBox1.Text == "GET")
            {
                r = Http.MakeGetRequest(textBox1.Text, ref response);
            }
            else if (comboBox1.Text == "POST")
            {
                byte [] postData = encoding.GetBytes(textBox3.Text);
                r = Http.MakePostRequest(textBox1.Text, postData, ref response);
            }
            else if (comboBox1.Text == "PUT")
            {
                byte[] putData = encoding.GetBytes(textBox3.Text);
                r = Http.MakePutRequest(textBox1.Text, putData, ref response);
            }
            else if (comboBox1.Text == "DELETE")
            {
                r = Http.MakeDeleteRequest(textBox1.Text, ref response);
            }
            else
            {
                textBox2.Text = "Invalid Method";
                return;
            }

            // Check if http request was successful
            if (!Validation.OfResult(r))
            {
                textBox2.Text = "Request failed. " + response;
                return;
            }

            // Output
            textBox2.Text = response;
            return;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void Home_Load(object sender, EventArgs e)
        {

        }
    }
}
